<?php
/***************************************************************
*  Copyright notice
*
*  (c) 2010 Grzegorz Bańka <grzegorz.banka@gmail.com>
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
 * [CLASS/FUNCTION INDEX of SCRIPT]
 *
 * Hint: use extdeveval to insert/update function index above.
 */

require_once(PATH_tslib.'class.tslib_pibase.php');


/**
 * Plugin 'MT price list' for the 'mt_prize' extension.
 *
 * @author	Grzegorz Bańka <grzegorz.banka@gmail.com>
 * @package	TYPO3
 * @subpackage	tx_mtprize
 */
class tx_mtprize_pi2 extends tslib_pibase {
	var $prefixId      = 'tx_mtprize_pi2';		// Same as class name
	var $scriptRelPath = 'pi2/class.tx_mtprize_pi2.php';	// Path to this script relative to the extension dir.
	var $extKey        = 'mt_prize';	// The extension key.
	var $pi_checkCHash = true;
	
	/**
	 * The main method of the PlugIn
	 *
	 * @param	string		$content: The PlugIn content
	 * @param	array		$conf: The PlugIn configuration
	 * @return	The content that is displayed on the website
	 */
	function main($content, $conf) {
		$this->conf = $conf;
		$this->pi_setPiVarDefaults();
		$this->pi_loadLL();
		
		$fe_user=$GLOBALS["TSFE"]->fe_user->user['uid'];
		$content=$this->get_list_price($fe_user);
	
		return $this->pi_wrapInBaseClass($content);
	}
	function get_list_price($user)
	{
		$this->templateCode = $this->cObj->fileResource($this->conf['templateFile']);
		$template['template'] = $this->cObj->getSubpart($this->templateCode,'###LIST###');
		$template['row'] = $this->cObj->getSubpart($template['template'],'###ROW###');

		$where = 'deleted=0 AND hidden=0 AND fe_user='.$user;
		$res =  $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid,crdate,point,description,add_point,what_columnt,what_uid,get_user','tx_mtprize_history',$where,'','uid DESC','0,'.$this->conf['show_max']);
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
		{
			$markerArray=array(
				'###OPTION###'	=>	$row['add_point']==0?'-':'+',
				'###POINT###'	=>	$row['point'],
				'###DESC###'	=>	$row['description'],
				'###WHAT###'	=>	$this->what_columnt($row['what_columnt'],$row['get_user'],$row['what_uid']),
				'###LINK###'	=>	$this->price_link($row['what_uid'],$row['what_columnt']),
				'###CLASS###'	=>	$row['add_point']==0?'minus':'plus',
				'###DATE###'	=>	date($this->conf['date'],$row['crdate'])
				);
			$subpartArray['###ROW###'][]=$this->cObj->substituteMarkerArrayCached($template['row'],$markerArray);		
		}

		if($subpartArray['###ROW###'][0]!='')$subpartArray['###ROW###']=implode("\n",$subpartArray['###ROW###']);
		else $subpartArray['###ROW###']='<tr><td>Brak historii dla punktów</td></tr>';
		
		return $this->cObj->substituteMarkerArrayCached($template['template'],$markerArray,$subpartArray);
		
	}
	function price_link($uid,$table)
	{
		return '-';
	}
	function what_columnt($what_columnt,$get='',$uid='')
	{
	
		switch($what_columnt)
		{
			case 'tx_mtwww_www':
				if($get==0)
				{
					$where = 'deleted=0 AND hidden=0 AND get_user='.$uid;
					$res =  $GLOBALS['TYPO3_DB']->exec_SELECTquery('point,what_columnt','tx_mtprize_history',$where);
					while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
					{
						switch($row['what_columnt'])
						{
							case 'tx_mtwww_view':
								$add[]='Za wygląd ('.$row['point'].')';
							break;
							
							case 'tx_mtwww_template':
								$add[]='Za szablon ('.$row['point'].')';
							break;
						}
					}
				}
				return implode("\n",$add);
			break;
		}
		return 'Brak informacji';
	}
}



if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/mt_prize/pi2/class.tx_mtprize_pi2.php'])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/mt_prize/pi2/class.tx_mtprize_pi2.php']);
}

?>